import 'package:flutter/material.dart';

class TurnBox extends StatefulWidget {
  final double turns; // 圈数
  final int speed; // 动画执行时长
  final Widget child;

  const TurnBox({Key key, this.turns, this.speed, this.child})
      : super(key: key);

  @override
  _TurnBoxState createState() => _TurnBoxState();
}

class _TurnBoxState extends State<TurnBox> with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
        vsync: this, lowerBound: -double.infinity, upperBound: double.infinity);
    _controller.value = widget.turns;
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return RotationTransition(
      turns: _controller,
      child: widget.child,
    );
  }

  @override
    void didUpdateWidget(TurnBox oldWidget) {
      super.didUpdateWidget(oldWidget);
      if(oldWidget.turns != widget.turns) {
        _controller.animateTo(
          widget.turns,
          duration: Duration(microseconds: widget.speed??200),
          curve: Curves.elasticOut,
        );
      }
    }
}
